package pure.sound
{
	import flash.events.Event;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	

public class MusicSound extends SoundItem
{
	public function MusicSound()
	{
		super();
		this._position=0;
	}
	
	//protected var _soundChannel			:SoundChannel;			/*音轨*/
	protected var _isLoop					:Boolean;					/*是否循环*/
	protected var _position					:Number;					/*当前播放到的时间(ms)*/
	
	/**
	 * 从开始播放
	 * @isLoop				是否循环
	 * <TODO>提供<单次播放|无限循环>两种播放状态
	 */	
	public function play(isLoop:Boolean=true):void
	{
		//super.play();
		/*if(!this._isPreload){
			loadSound();
		}*/
		
		var loop:int=isLoop?0:1;
		var soundChannel:SoundChannel= this._sound.play(0, loop, this._soundTransform);
		if(soundChannel){
			if(this._soundChannel){
				this._soundChannel.removeEventListener(Event.SOUND_COMPLETE,____onPlayFinished);
				this._soundChannel.stop();
			}
			this._soundChannel=soundChannel;
			if(this._isLoop!=isLoop){
				this._isLoop=isLoop;
			}
			if(!this._isLoop){
				this._soundChannel.addEventListener(Event.SOUND_COMPLETE, ____onPlayFinished);
			}
			if(!this._isPlaying){
				this._isPlaying = true;
			}
		}
		else{
			throw new Error("<URL::"+this._url+">声音文件出错!");
		}
	}
	
	/**
	 * 继续上次播放
	 */	
	public function doContinue():void
	{
		if(this._isPlaying&&!this._position){
			return;
		}
		var loop:int=this._isLoop?0:1;
		var soundChannel:SoundChannel= this._sound.play(this._position, loop, this._soundTransform);
		if(soundChannel){
			this._soundChannel=soundChannel;
			if(!this._isLoop){
				this._soundChannel.addEventListener(Event.SOUND_COMPLETE, ____onPlayFinished);
			}
			this._isPlaying = true;
		}
		else{
			throw new Error("<URL::"+this._url+">声音文件出错!");
		}
	}
	
	public function stop(isPaused:Boolean=false):void
	{
		this._position=isPaused?this._soundChannel.position:0;
		//super.stop();
		if(!this._isPlaying){
			return;
		}
		this._isPlaying = false;
		
		this._soundChannel.removeEventListener(Event.SOUND_COMPLETE, ____onPlayFinished);
		this._soundChannel.stop();
		this._soundChannel=null;
	}
	
	public function clear():void 
	{
		if (this._isPlaying) {
			stop();
		}
		this._sound.close();
	}
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	

	/*调节音量*/
	override public function set volume(value:Number):void
	{
		super.volume=value;
		if(this._soundChannel){
			this._soundChannel.soundTransform=this._soundTransform;
		}
	}
	
	/*当前播放到的时间*/
	public function set position(value:Number):void
	{
		if(value<0&&value>this._sound.length){
			throw new Error("<MusicSound>播放位置超出范围!")
		}
		this._position=value;
	}
	
	/*<only-read>当前播放到的时间*/
	public function get position():Number
	{
		return this._position;
	}
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	

	/*<非循环模式下>声音播放结束*/
	protected function ____onPlayFinished(e:Event) : void
	{
		this._soundChannel.removeEventListener(Event.SOUND_COMPLETE, ____onPlayFinished);
		this._soundChannel=null;
		this._isPlaying = false;
		//SoundManager.instance.dispatchEvent(new SoundEvent(SoundEvent.PLAY_FINISHED));
	}
}
}